package com.xckj.cycling.adapter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.inuker.bluetooth.library.Constants;
import com.xckj.cycling.R;
import com.xckj.cycling.bean.DeviceAddedResponseBean;
import com.xckj.cycling.bluetooth.BluetoothClientManager;
import com.xckj.cycling.utils.DetectUtil;

import java.util.List;

/**
 * @Author: ChenYao
 * @Describe: 已添加设备
 * @Date: 2023/3/18 19:52
 */
public class DeviceAddedAdapter extends RecyclerView.Adapter<DeviceAddedAdapter.MyViewHolder> {
    private List<DeviceAddedResponseBean.ListBean> mDeviceAdded;
    private OnItemClickListener onItemClickListener;


    @SuppressLint("NotifyDataSetChanged")
    public void setData(List<DeviceAddedResponseBean.ListBean> deviceAdded) {
        mDeviceAdded = deviceAdded;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_device_added, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        DeviceAddedResponseBean.ListBean device = mDeviceAdded.get(position);
        holder.tvDeviceName.setText(device.bname);

        try {
            int connectStatus = BluetoothClientManager.getBluetoothClient().getConnectStatus(mDeviceAdded.get(position).sn_mac);
            if (connectStatus == Constants.STATUS_DEVICE_CONNECTED || connectStatus == Constants.STATUS_DEVICE_CONNECTING) {
                // 已连接
                holder.tvStatus.setTextColor(Color.parseColor("#A6A6A6"));
                holder.tvStatus.setText(R.string.connected);
                holder.tvStatus.setEnabled(false);
            }else {
                // 未连接
                holder.tvStatus.setTextColor(Color.parseColor("#777373"));
                holder.tvStatus.setText(R.string.connect);
                holder.tvStatus.setEnabled(true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        holder.tvStatus.setOnClickListener(view -> {
            if(TextUtils.isEmpty(device.sn_mac)){
                Toast.makeText(view.getContext(), "返回的Mac地址是空的！", Toast.LENGTH_SHORT).show();
                return;
            }
            if(!DetectUtil.isValidMac(device.sn_mac)){
                Toast.makeText(view.getContext(), "返回的Mac地址不合法！", Toast.LENGTH_SHORT).show();
                return;
            }

            int connectStatus = BluetoothClientManager.getBluetoothClient().getConnectStatus(device.sn_mac);
            if (connectStatus == Constants.STATUS_DEVICE_CONNECTED || connectStatus == Constants.STATUS_DEVICE_CONNECTING) {
                Toast.makeText(view.getContext(), view.getResources().getString(R.string.connected), Toast.LENGTH_SHORT).show();
                return;
            }

            if (null != onItemClickListener) {
                onItemClickListener.onItemClick(device.sn_mac);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDeviceAdded.size();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        private final TextView tvDeviceName;
        private final TextView tvStatus;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tvDeviceName = itemView.findViewById(R.id.tv_device_name);
            tvStatus = itemView.findViewById(R.id.tv_status);
        }
    }

    public interface OnItemClickListener {
        /**
         * Item的点击事件
         *
         * @param sn_mac mac地址
         */
        void onItemClick(String sn_mac);
    }

}
